package com.school.hjt.schoolclasscard.fragment

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.util.Log
import android.view.View
import android.widget.LinearLayout
import android.widget.ListView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.school.hjt.schoolclasscard.R
import com.school.hjt.schoolclasscard.adapter.NewsAdapter
import com.school.hjt.schoolclasscard.db.SchArticle
import com.school.hjt.schoolclasscard.model.HomeFModel
import com.school.hjt.schoolclasscard.ui.AttendancePage
import com.school.hjt.schoolclasscard.ui.LabourActivity
import com.school.hjt.schoolclasscard.utlis.ClassCalendar
import com.school.hjt.schoolclasscard.utlis.Constant
import com.school.hjt.schoolclasscard.utlis.GlideImageLoader
import com.youth.banner.Banner
import com.youth.banner.Transformer
import kotlinx.android.synthetic.main.fragment_home.*
import java.util.*
import java.util.Locale.filter
import kotlin.collections.ArrayList

/**
 * Created by Administrator on 2017/6/25.
 *
 */
class TabHomeFragment : BaseFragment(){
    var timer:Timer?=null
    var fHomeReceiver:FHomeReceiver?=null
    var model:HomeFModel?=null
    var ada:NewsAdapter?=null
    var article:SchArticle?=null
    override fun initView(view: View?) {
        banner = view!!.findViewById(R.id.banner)
        banner1 = view.findViewById(R.id.banner1)
        val ll_labour=view.findViewById<LinearLayout>(R.id.ll_labour)
        val lv_news=view.findViewById<ListView>(R.id.lv_news)
        ll_labour.setOnClickListener({
            val intent= Intent(activity,LabourActivity::class.java)
            activity.startActivity(intent)
        })

        timer = Timer()
//        timer!!.schedule(object : TimerTask() {
//            override fun run() {
//                activity.runOnUiThread {
//                    tv_classtime.text = ClassCalendar.getIntTimes() }
//            }
//        }, 1000, 20000)
        banner!!.setOnBannerListener {
            val intent=Intent()
            intent.action=Constant.SCHBANNER
            activity.sendBroadcast(intent)
        }
//        lv_news.setOnClickListener {
//            Log.e("ATG","点击")
//        }
        lv_news.setOnItemClickListener { adapterView, view, i, l ->
            Log.e("ATG","点击")
            val intent=Intent()
            intent.action=Constant.SCHARTICLR
            intent.putExtra(Constant.SCHARTICLR,postion)
            activity.sendBroadcast(intent)
//            lv_news.requestFocus()
        }

    }
    var num=0
    var postion=0
    override fun loadData() {
        fHomeReceiver=FHomeReceiver()
        model= HomeFModel()
        tv_classtime.text=ClassCalendar.getHomeWeek()
        tv_Motto.text=model!!.motto
        if (model!!.schoolImg != "") {
            Glide.with(this).load(model!!.schoolImg).into(tv_SchoolImg)
        }
        activity.registerReceiver(fHomeReceiver,filter())
        banner!!.setImageLoader(GlideImageLoader())

        banner!!.setImages(model!!.album)
        banner!!.setBannerAnimation(Transformer.CubeOut)
        banner!!.setDelayTime(6000)
//        banner.setBannerAnimation(Transformer.ZoomIn)
        //banner设置方法全部调用完毕时最后调用
        banner!!.start()
        banner1!!.setImageLoader(GlideImageLoader())
        //设置图片集合
//        Log.e("ATG",model!!.classAlbum[0])
        banner1!!.setImages(model!!.classAlbum)
        banner1!!.setBannerAnimation(Transformer.CubeOut)
        banner1!!.setDelayTime(6000)
//        banner.setBannerAnimation(Transformer.ZoomIn)
        //banner设置方法全部调用完毕时最后调用
        banner1!!.start()
//        article = model!!.article
        val list1=ArrayList<SchArticle>()

        val str=model!!.article
        if (str.size!=0){
            list1.add(str[num])
            postion=num
            ++num
            ada=NewsAdapter(activity,list1)
            lv_news.adapter=ada
        }
        timer!!.schedule(object : TimerTask() {
            override fun run() {
                activity.runOnUiThread {
                    list1.clear()
//                    Log.e("ATG",""+str.size+"   "+num)
                    val str=model!!.article
                    if(str.size!=0){
                        if (str.size>num){
//                        Log.e("ATG",""+"  | "+num)
                            list1.add(str[num])
                            postion=num
                            ++num
                        }else{
                            num=0
//                            Log.e("ATG",""+"  | "+num)
                            list1.add(str[num])
                            postion=num
                            ++num
                        }
                    }
                    if (ada!=null){
                        ada!!.notifyDataSetChanged()
                    }else{
                        ada=NewsAdapter(activity,list1)
                        lv_news.adapter=ada
                    }
                }
            }
        }, 10000, 10000)
        artSchool()
        marqueeView.startWithList(artSchool)
        Adm_teacher()
        marqueeView1.startWithList(adm_teacher)
        Lab_teacher()
        marqueeView2.startWithList(lab_teacher)
        val labours = model!!.labours
        tv_labour.text="值日班级 : "+labours.classname;
        tv_labourTime.text=labours.now_date
        tv_ClassMotto.text=model!!.classMotto
//
        getTo()
//        Log.e("ATG",labours.classname+" 时间"+labours.now_date)
    }
    var banner: Banner?=null
    var banner1: Banner?=null
    override fun getLayoutResource(): Int {
        return R.layout.fragment_home
    }

    override fun onDetach() {
        banner!!.stopAutoPlay()
        if (timer != null) {
            timer!!.cancel()
            timer = null
        }
        super.onDetach()
    }
    /**
     * 校园动态
     */
    val artSchool=ArrayList<String>()
    fun artSchool() {
        artSchool.clear()
        val artClass = model!!.ArtSchools()
        artClass.mapTo(artSchool) { it.trim() }
    }
    /**
     *
     */
    val lab_teacher=ArrayList<String>()
    fun Lab_teacher() {
        lab_teacher.clear()
        val teacher = model!!.labTeacher
        Log.e("DATA",teacher[0])
        for(i in teacher.indices){
            lab_teacher.add(teacher[i])
        }
//        teacher.mapTo(lab_teacher) { it.trim() }
    }
    /**
     *
     */
    val adm_teacher=ArrayList<String>()
    fun Adm_teacher() {
        adm_teacher.clear()
        val teacher = model!!.admTeacher
        teacher.mapTo(adm_teacher) { it.trim() }
    }

    /**
     * 广播类
     */
    inner class FHomeReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            val action = intent.action
            when (action) {
                Constant.WEATHER->{
                   val weather = intent.getStringExtra(Constant.WEATHER)
                   tv_Weather.text=weather
                }
                Constant.INFO->{
                    tv_Motto.text=model!!.motto
                    Glide.with(activity).load(model!!.schoolImg).into(tv_SchoolImg)
                }
                Constant.ALBUM->{
                    banner!!.update(model!!.album)
                }
                Constant.ARTSCHOOL->{
                    artSchool()
                    marqueeView!!.startWithList(artSchool)
                }
                Constant.LABOUR->{
                    val labours = model!!.labours
                    tv_labour.text="值日班级 : "+labours.classname;
                    tv_labourTime.text=labours.now_date
                    tv_ClassMotto.text=model!!.classMotto
                    Lab_teacher()
                    marqueeView1.startWithList(lab_teacher)
                    Adm_teacher()
                    marqueeView2.startWithList(adm_teacher)
                    banner1!!.update(model!!.classAlbum)
                }
                Constant.KQ->{
                    Log.e("ATG","进来考勤")
                    getTo()
                }
            }
        }
    }
        /**
         * @return IntentFilter 广播
         */
        private fun filter(): IntentFilter {
            val filter = IntentFilter()
            filter.addAction(Constant.WEATHER)
            filter.addAction(Constant.INFO)
            filter.addAction(Constant.ALBUM)
            filter.addAction(Constant.ARTSCHOOL)
            filter.addAction(Constant.LABOUR)
            filter.addAction(Constant.KQ)
            return filter
        }
    private fun getTo(){
        if(Constant.isTS){
            tv_Num.text=model!!.num.toString()
            tv_To.text=model!!.getTo(activity,1)
            tv_noTo.text=model!!.getTo(activity,2)
        }else{
            tv_Num.text=model!!.num.toString()
            tv_To.text=model!!.to.toString()
            tv_noTo.text=(model!!.num-model!!.to).toString()
            ll_Kq.setOnClickListener({
                            val intent=Intent(activity,AttendancePage::class.java)
            activity.startActivity(intent)
        })
        }
//        tv_Num.text=model!!.num.toString()
//        tv_To.text=model!!.getTo(activity,1)
//        tv_noTo.text=model!!.getTo(activity,2)
//        tv_To.text=model!!.to.toString()
//        tv_noTo.text=(model!!.num-model!!.to).toString()
    }
}